package com.ferryc.controller;

import com.ferryc.entity.ProductCode;
import com.ferryc.entity.Products;
import com.ferryc.model.ProductTotal;
import com.ferryc.model.Result;
import com.ferryc.service.IProductCodeService;
import com.ferryc.service.IProductsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * Created by Ferry Chen on 2019-11-05.
 */
@Api(tags = {"商品接口"})
@Slf4j
@RestController
@RequestMapping(value = "/product",produces = MediaType.APPLICATION_JSON_VALUE)
public class ProductsController {


    @Autowired
    private IProductsService productsService;
    @Autowired
    private IProductCodeService productCodeService;

    /**
     * 查询商品品类信息
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "查询商品分类", notes = "查询商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "商品分类编号", required = true, dataType = "String"),
    })
    @GetMapping(value = "/queryProductCode")
    public Result<List<ProductCode>> queryProductCode(@RequestParam(value = "code") String code) {
        ProductCode pc = new ProductCode();
        pc.setCode(code);
        try {
            return Result.success(productCodeService.queryList(pc));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    @ApiOperation(value = "新增商品分类", notes = "新增商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "商品分类编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "codeName", value = "商品分类名称", required = true, dataType = "String")
    })
    @PostMapping("/addProductCode")
    public Result addProductCode(@RequestParam(value = "code") String code,
                                 @RequestParam(value = "codeName") String codeName) {
        ProductCode pc = new ProductCode();
        pc.setCode(code);
        pc.setName(codeName);
        try {
            productCodeService.save(pc);
            return Result.success("success");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail("fail");
        }
    }

    /**
     * 查询商品信息
     *
     * @param code
     * @return
     */
    @ApiOperation(value = "查询商品信息", notes = "查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "商品分类编号", required = false, dataType = "String"),
    })
    @GetMapping(value = "/queryProducts")
    public Result<List<Products>> queryProducts(@RequestParam(value = "code",required = false) String code) {
        Products pc = new Products();
        pc.setProductCode(code);
        try {
            return Result.success(productsService.queryList(pc));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 查询更新商品统计信息
     */
    @ApiOperation(value = "查询更新商品统计信息", notes = "查询更新商品统计信息")
    @GetMapping(value = "/queryProductTotal")
    public Result<List<ProductTotal>> queryProductTotal() {
        try {
            return Result.success(productsService.selectTotalByCode());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail("fail");
        }
    }

    /**
     * 新增/更新商品统计信息
     */

    @ApiOperation(value = "新增/更新商品统计信息", notes = "新增/更新商品统计信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "商品分类编号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "price", value = "价格", required = false, dataType = "number"),
            @ApiImplicitParam(name = "productName", value = "商品名称", required = false, dataType = "String"),
            @ApiImplicitParam(name = "productId", value = "商品ID", required = false, dataType = "String"),
    })
    @PostMapping("/save")
    public Result save(String code, double price, String productName, String productId) {
        Products products = new Products();
        products.setId(productId);
        products.setProductCode(code);
        products.setPrice(price);
        products.setProductName(productName);
        try {
            productsService.saveOrUpdate(products);
            return Result.success("ok");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail("fail");
        }
    }

}
